<!DOCTYPE html>
<html lang="zh" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>文件预览跳转中...</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
  </head>
  <body>
  </body>
  <script>
    (function (){
      var path = '<?php echo @$_GET["path"];?>';
      var name = '<?php echo @$_GET["name"];?>';
      //以下配置需要从ajax读取
      var pluginHost = "<?php echo $this->pluginHost;?>";
      var tmpPhosts = pluginHost.split("/");
      if(!tmpPhosts[tmpPhosts.length-1]){
        tmpPhosts.length = tmpPhosts.length-1;
        pluginHost = tmpPhosts.join("/");
      }
      var filePage = pluginHost+"/editer-service/html/index.html";
      var sdk = pluginHost+"/static/sdk.js";
      var parentPath = "";
      var webHost = window.location.origin+window.location.pathname;
      var util = {
        ajax:function(options){
          if(!options.type){
            options.type = "GET";
          }
          if(!options.dataType){
            options.dataType = "text";
          }
          var xmlhttp;
          if (window.XMLHttpRequest){
            xmlhttp=new XMLHttpRequest();
          }else{
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
          xmlhttp.onreadystatechange=function(){
            if(xmlhttp.readyState==4){
              try{
                if(options.success){
                  var res = xmlhttp.responseText;
                  if(options.dataType == "json"){
                    res = JSON.parse(res);
                  }
                  options.success(res);
                }
              }catch (e) {
                console.warn(e);
              }
            }
          }
          xmlhttp.open(options.type,options.url,true);
          if(options.contentType){
            xmlhttp.setRequestHeader("content-type",options.contentType);
          }
          xmlhttp.send(options.data);
        },
        getCookie:function(key){
          var arr = document.cookie.match(new RegExp("(^| )"+key+"=([^;]*)(;|$)"));
          if(arr != null){
            return unescape(arr[2]);
          }else{
            return null;
          }
        },
        getFileUrl:function(fn){
          if(parent.location.hash.startsWith("#s/")){
            //分享页面
            var hashsz = parent.location.hash.split("/");
            var shareId = hashsz[hashsz.length-1];
            hashsz = shareId.split("&");
            shareId = hashsz[0];
            var url = webHost+"?explorer/share/fileDownload&shareID="+encodeURIComponent(shareId)+"&path="+encodeURIComponent(path);
            fn(url);
          }else{
            //默认页
            var infoData = "dataArr="+encodeURIComponent('[{"path":"'+path+'"}]')+"&CSRF_TOKEN="+encodeURIComponent(this.getCookie("CSRF_TOKEN"))+"&API_ROUTE="+encodeURIComponent("explorer/index/pathInfo");
            this.ajax({
              url:webHost+"?explorer/index/pathInfo",
              contentType:"application/x-www-form-urlencoded; charset=UTF-8",
              dataType:"json",
              type:"POST",
              data:infoData,
              success:function(res){
                if(!res.code){
                  return;
                }
                var fileUrl = res.data.downloadPath;
                if(res.data.parentID){
                  parentPath = "{source:"+res.data.parentID+"}/";
                }else{
                  var pathsz = res.data.path.split("/");
                  pathsz.length =pathsz.length -1;
                  parentPath = pathsz.join("/")+"/";
                }
                fn(fileUrl);
              }
            });
          }
        },
        actionUrl:function(){
          var that = this;
          that.getFileUrl(function (fileUrl){
            var data = {
              path:path
            };
            that.ajax({
              url:pluginHost+"/php/action.php?type=6",
              contentType:"application/json; charset=UTF-8",
              dataType:"json",
              type:"POST",
              data:JSON.stringify(data),
              success:function(res){
                if(res.flag){
                  var file_id = "";
                  if(res.data){
                    file_id = "&file_id="+ encodeURIComponent(res.data);
                  }
                  var search = "?url="+encodeURIComponent(fileUrl)+"&name="+encodeURIComponent(name)+"&sdk="+encodeURIComponent(sdk)+"&parentPath="+ encodeURIComponent(parentPath)+"&path="+ encodeURIComponent(path)+file_id;
                  window.location.href = filePage + search;
                }else{
                  alert(res.msg);
                }
              }
            });
          });
        }
      }
      util.actionUrl();
    })()
  </script>
</html>
